package com.erci.service.Impl;

import com.erci.entity.Anime;
import com.erci.entity.DTO.AnimeDTO;
import com.erci.entity.Episode;
import com.erci.entity.VO.AnimeDetailVO;
import com.erci.entity.VO.AnimeUserVO;
import com.erci.entity.VO.AnimeVO;
import com.erci.mapper.AnimeMapper;
import com.erci.mapper.EpisodeMapper;
import com.erci.result.PageResult;
import com.erci.service.AnimeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class AnimeServiceImpl implements AnimeService {
    @Autowired
    private AnimeMapper animeMapper;
    @Autowired
    private EpisodeMapper episodeMapper;

    @Override
    public PageResult queryList(Integer page, Integer pageSize, String name, List<Integer> categoryIds, String status) {
        // 第一步：查询满足条件的 anime id 列表
        List<Integer> animeIds = animeMapper.queryIdsByCondition(name, categoryIds, status);

        if (animeIds.isEmpty()) {
            return new PageResult(Collections.emptyList(), 0);
        }
        PageHelper.startPage(page, pageSize);

        // 第二步：查询完整数据（包含所有分类）
        Page<AnimeVO> pageList = animeMapper.queryListByIds(animeIds);
        long total = pageList.getTotal();
        return new PageResult(pageList.getResult(),total );
    }
    //

    @Override
    @Transactional
    public void add(AnimeDTO animeDTO) {
        animeDTO.setCreateTime(LocalDateTime.now());
        animeDTO.setUpdateTime(LocalDateTime.now());
        Anime anime = new Anime();
        BeanUtils.copyProperties(animeDTO,anime);
        anime.setEpisodes(0);
        // 插入anime库
        animeMapper.add(anime);
        Integer animeId = anime.getId();
        if ( animeId!= null) {
            // 插入anime_category库
            animeMapper.addAnimeCategory(animeId, animeDTO.getCategoryIds());
        }
    }

    @Override
    @Transactional
    public void delete(AnimeDTO animeDTO) {
        // 删除anime库
        animeMapper.deleteById(animeDTO.getId());
        // 删除anime_category库
        animeMapper.deleteAnimeCategory(animeDTO.getId());
    }

    @Override
    public void update(AnimeDTO animeDTO) {
        // 更新anime库
        animeDTO.setUpdateTime(LocalDateTime.now());
        Anime anime = new Anime();
        BeanUtils.copyProperties(animeDTO,anime);
        animeMapper.update(anime);
        //删除anime_category库
        animeMapper.deleteAnimeCategory(animeDTO.getId());
        // 插入anime_category库
        animeMapper.addAnimeCategory(animeDTO.getId(), animeDTO.getCategoryIds());
    }
    //////////////////////////////////////////////////////////////////////////////////////////////

    @Override
    public PageResult queryListUser(Integer page, Integer pageSize, String name, Integer categoryId, String year) {
        // 第一步：查询满足条件的 anime id 列表
        List<Integer> animeIds = animeMapper.queryListUser(name, categoryId, year);

        if (animeIds.isEmpty()) {
            return new PageResult(Collections.emptyList(), 0);
        }
        PageHelper.startPage(page, pageSize);

        // 第二步：查询完整数据（包含所有分类）
        Page<AnimeUserVO> pageList = animeMapper.queryListUserByIds(animeIds);

        long total = pageList.getTotal();
        return new PageResult(pageList.getResult(),total );
    }

    @Override
    public AnimeDetailVO queryDetail(Integer id) {
        AnimeDetailVO animeDetailVO =animeMapper.queryDetail(id);
        return animeDetailVO;
    }
    @Override
    public List<Episode> queryEpisodes(Integer id) {
        //1 查询所有episode_ids
        List<Integer> episodeIds = episodeMapper.queryEpisodeIds(id);
        //2 根据episode_ids查询episodes
        List<Episode> episodes = episodeMapper.queryEpisodesByIds(episodeIds);
        return episodes;
    }
}
